﻿using RestSharp;
using ServiceStack.Text;
using System;
using System.Collections.Generic;
using System.Web;
using Ymatou.ServiceCenter;
using YmtAuth.AppService;
using YmtAuth.Domain.Repository;
using YmtAuth.Domain.Repository.EventRepository;
using YmtAuth.Web.Shard;
using YmtSystem.CrossCutting;

namespace YmtAuth.Web.Handler
{
     
    public class warmupHandler : IHttpHandler
    {
        public bool IsReusable
        {
            get { return false; }
        }

        [ServiceRegister("warmup", Method.GET)]
        public void ProcessRequest(HttpContext context)
        {
            try
            {
                YmatouLoggingService.Debug("开始点火");

                LocalServiceLocator.GetService<IUserLoginBillsRepository>();
                LocalServiceLocator.GetService<IYmtUserLoginHistoryRepository>();
                LocalServiceLocator.GetService<ILoginIdUpdateEventRepository>();
                LocalServiceLocator.GetService<IUserTokenRepository>();

                LocalServiceLocator.GetService<IBlackListRepository>();
                LocalServiceLocator.GetService<IAuthConfigurationRepository>();
                LocalServiceLocator.GetService<IYmatouBlacklistUserRepository>();

                YmatouLoggingService.Debug("点火 成功");

                if (context == null) return;
                context.Response.ContentType = "text/html; charset=utf-8";
                context.Response.StatusCode = 200;
                context.Response.Write("ok");                
            }
            catch (Exception ex)
            {
                string errMsg = string.Format("点火失败: {0}", ex.Message);

                YmatouLoggingService.Error(errMsg, ex);
                context.Response.ContentType = "text/html; charset=utf-8";
                context.Response.StatusCode = 200;
                string errResponse = "{" + string.Format("string:\"{0}\"", ex.Message) + "}";
                context.Response.Write(errResponse);
            }
        }
    }
}